package yxy.game.pm2.command.niubee;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.weaponrefine;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.NiuBeeMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.niubee.NiuBeeModule;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.Collection;

/** 神器 神器精炼 */
@MessageType(proto = Command.NiuBeeRefinement, classtype = NiuBeeMessage.NiuBeeRefinementRequest.class)
public class CMD_NiuBeeRefinement extends Command<NiuBeeMessage.NiuBeeRefinementRequest> {

	@Override
	public void execute(MessageSource messageSource, NiuBeeMessage.NiuBeeRefinementRequest request) {
		String playerCode = messageSource.getTag();

		NiuBeeMessage.NiuBeeRefinementResponse.Builder response = NiuBeeMessage.NiuBeeRefinementResponse.newBuilder();

		NiuBeeModule niuBeeModule = NiuBeeModule.get(playerCode);
		try {
			if (niuBeeModule.tryLock()) {
				int refine = niuBeeModule.getRefineLevel();
				weaponrefine data = DataCenter.getData(refine + 1, weaponrefine.class);
				if (data == null) {
					send(messageSource, response, Result.NiubeeRefineUpper);// 满级
					return;
				}
				data = DataCenter.getData(refine, weaponrefine.class);
				Collection<Currency> costs = Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum());
				try {
					new CurrencyHandle().cost(playerCode, CCReason.神器, costs);
				} catch (CurrencyNotEnoughException e) {
					send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
					return;
				}
				niuBeeModule.setRefineLevel(refine + 1);
				niuBeeModule.set();

				response.setRefine(niuBeeModule.getRefineLevel());
				send(messageSource, response, Result.Success);


				PlayTypeHandle.saveWeapon(playerCode, PlayTypeModule.get(playerCode));
				new BattleHandle().powerUpdate(playerCode);// 战力更新

				if (DataCenter.getData(niuBeeModule.getRefineLevel() + 1, weaponrefine.class) == null) {
					new TipHandle().updateTip(playerCode, TipType.神器_精炼切页, false); // 满级
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			niuBeeModule.unlock();
		}
	}

	private void send(MessageSource messageSource, NiuBeeMessage.NiuBeeRefinementResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}